﻿using System.Collections.Generic;
using UnityEngine;

namespace ZFramework.Runtime
{
    public class EntityGroup : MonoBehaviour, IEntityGroup
    {
        private string _name;
        private LinkedList<Entity> _entities;
        private ObjectPoolMgr.ObjectPool<EntityInstanceObject> _instancePool;
        private ObjectPoolMgr _objectPoolMgr;

        public string Name => _name;
        public GameObject Root => gameObject;

        public int EntityCount
        {
            get { return _entities.Count; }
        }

        public float InstanceAutoReleaseInterval { get; set; }
        public int InstanceCapacity { get; set; }
        public float InstanceExpireTime { get; set; }
        public int InstancePriority { get; set; }

        public void Init(string name, float instanceAutoReleaseInterval, int instanceCapacity, float instanceExpireTime,
            int instancePriority, ObjectPoolMgr objectPoolMgr)
        {
            _name = name;
            _entities = new LinkedList<Entity>();
            _objectPoolMgr = objectPoolMgr;
            _instancePool = _objectPoolMgr.CreateSingleSpawnObjectPool<EntityInstanceObject>(name,
                instanceAutoReleaseInterval,
                instanceCapacity, instanceExpireTime);
        }

        public void DeInit()
        {

        }

        public void OnUpdate(float elapseSeconds, float realElapseSeconds)
        {
            foreach (var entity in _entities)
            {
                entity.OnUpdate(elapseSeconds, realElapseSeconds);
            }
        }


        public bool HasEntity(int entityId)
        {
            foreach (var entity in _entities)
            {
                if (entity.Id == entityId)
                    return true;
            }

            return false;
        }

        public bool HasEntity(string entityPath)
        {
            foreach (var entity in _entities)
            {
                if (entity.EntityPath == entityPath)
                    return true;
            }

            return false;
        }

        public Entity GetEntity(int entityId)
        {
            foreach (var entity in _entities)
            {
                if (entity.Id == entityId)
                    return entity;
            }

            return null;
        }

        public Entity GetEntity(string entityPath)
        {
            foreach (var entity in _entities)
            {
                if (entity.EntityPath == entityPath)
                    return entity;
            }

            return null;
        }

        public Entity[] GetEntities(string entityPath)
        {
            List<Entity> results = new List<Entity>();
            foreach (Entity entity in _entities)
            {
                if (entity.EntityPath == entityPath)
                {
                    results.Add(entity);
                }
            }

            return results.ToArray();
        }

        public void GetEntities(string entityPath, List<Entity> results)
        {
            results.Clear();
            foreach (Entity entity in _entities)
            {
                if (entity.EntityPath == entityPath)
                {
                    results.Add(entity);
                }
            }
        }

        public Entity[] GetAllEntities()
        {
            List<Entity> results = new List<Entity>();
            foreach (Entity entity in _entities)
            {
                results.Add(entity);
            }

            return results.ToArray();
        }

        public void GetAllEntities(List<Entity> results)
        {
            results.Clear();
            foreach (Entity entity in _entities)
            {
                results.Add(entity);
            }
        }

        public void AddEntity(Entity entity)
        {
            _entities.AddLast(entity);
        }

        public void RemoveEntity(Entity entity)
        {
            if (!_entities.Remove(entity))
            {
                return;
            }
        }

        //注册一个entityObject
        public void RegisterEntityInstanceObject(EntityInstanceObject obj, bool spawned)
        {
            _instancePool.Register(obj, spawned);
        }

        //从池子里拿出一个
        public EntityInstanceObject SpawnEntityInstanceObject(string name)
        {
            return _instancePool.Spawn(name);
        }

        public void UnSpawnEntity(Entity entity)
        {
            _instancePool.UnSpawn(entity.Obj);
        }
    }
}